#include "seq.h"

void SeqListInt(SeqList* list)
{
	assert(list);

	list->head = (SLDateType*)malloc(sizeof(SLDateType)*3);

	if (list->head == NULL)
		perror("malloc");

	list->sz = 0;
	list->capacity = 3;
}

void SeqListExpand(SeqList* head)
{
	assert(head);
	if(head->sz==head->capacity)
	{
		SLDateType* tmp = (SLDateType*)realloc(head->head, sizeof(SLDateType) * 2 * head->capacity);
		if (tmp == NULL)
		{
			perror("realloc");
			return;
		}
		head->head = tmp;
		head->capacity *= 2;
	}
}

void SeqListPrintf(SeqList* head)
{
	int i = 0;
	for (i = 0; i < head->sz; i++)
	{
		printf("%d ", head->head[i]);
	}
}

void SeqListPushBack(SeqList* head,SLDateType x)
{
		SeqListExpand(head);
	head->head[head->sz] = x;
	head->sz++;
}

void SeqListPushFront(SeqList* head, SLDateType x)
{
	assert(head);
		SeqListExpand(head);
		for (int i = head->sz-1; i >= 0; i--)
		{
			head->head[1+i] = head->head[i];
		}
	head->head[0] = x;
	head->sz++;
}

void SeqListPopBack(SeqList* head)
{
	assert(head);
	head->head[head->sz - 1] = 0;
	head->sz--;
}

void SeqListPopFront(SeqList* head)
{
	assert(head);
	int num = 0;
	while (num<head->sz)
	{
		head->head[num] = head->head[num + 1];
		num++;
	}
	head->sz--;
}

int SeqListFind(SeqList* head,SLDateType x)
{
	assert(head);
	for (int i = 0; i < head->sz; i++)
	{
		if (head->head[i] == x)
			return i;
	}
	return -1;
}

void SeqListInsert(SeqList* head, int pos, SLDateType x)
{
	assert(head);
	SeqListExpand(head);
	for (int i = head->sz - 1; i >= pos; i--)
		head->head[i + 1] = head->head[i];
	head->head[pos] = x;
	head->sz++;
}

void SeqListErase(SeqList* head, int pos)
{
	assert(head);
	for (int i = pos; i < head->sz; i++)
	{
		head->head[i] = head->head[i + 1];
	}
	head->sz--;
}

void SeqListDestroy(SeqList* list)
{
	free(list->head);
	list->head = NULL;
	list->sz = 0;
	list->capacity = 0;
}